package com.orange.controller.school;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.handler.AuthProofService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.AuthProof;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/proof")
public class AuthProofController extends BaseController{

	private static final Logger logger = Logger.getLogger(AuthProofController.class);
	
	@Autowired
	private AuthProofService authProofService;
	
	/**
	 * 添加佐证材料
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertProof" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertProof(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==pid){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			String subject=(String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);
			String number = map.get("number");
			String itemId = map.get("itemId");
			String filepath = map.get("filepath");
			String filename = map.get("filename");
			String dealTime = map.get("dealTime");
			String open = map.get("open");
			if(number==null || StringUtils.isBlank(number))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"序号不能为空");
			if(itemId==null || StringUtils.isBlank(itemId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"分项Id不能为空");
			if(filepath==null || StringUtils.isBlank(filepath))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"文件路径不能为空");
			if(filename==null || StringUtils.isBlank(filename))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"文件名称不能为空");
			if(dealTime==null || StringUtils.isBlank(dealTime))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"成交时间不能为空");
			if(open==null || StringUtils.isBlank(open))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"是否公开不能为空");
			
			String[] filepathList = filepath.split(",");
			String[] filenameList = filename.split(",");
			String[] dealTimeList = dealTime.split(",");
			String[] openList = open.split(",");
			String[] numberList = number.split(",");
			List<AuthProof> list=new ArrayList<AuthProof>();
			for(int i = 0;i<filepathList.length;i++){
				AuthProof ap = new AuthProof();
				ap.setCreatetime(sdf.format(new Date()));
				ap.setDealtime(dealTimeList[i]);
				ap.setFilename(filenameList[i]);
				ap.setFilepath(filepathList[i]);
				ap.setItemId(itemId);
				ap.setLoginType(subject);
				ap.setNumber(numberList[i]);
				ap.setOpen(openList[i]);
				ap.setSid(pid);
				list.add(ap);
			}
			authProofService.deleteAuthProofInfo(itemId,pid, subject);
			authProofService.insertAuthProof(list);
			return toResponse("",ReturnCode.SUCCESS,"新增成功");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"新增失败");
		}
		
	}
	
}
